package com.yaosheng.thread.lock;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by yaosheng on 2021/7/1.
 */
public class Test01 {

    volatile List lists = Collections.synchronizedList (new LinkedList<> ());
    //volatile List lists = new LinkedList<> ();

    public void add(Object o){
        lists.add (o);
    }

    public int size(){
        return lists.size ();
    }

    public static void main(String[] args) {

        Test01 test01 = new Test01 ();
        new Thread ( () -> {
            for(int i = 0;i < 10;i ++){
                test01.add (new Object ());
                System.out.println ("add :" + i);
                try {
                    TimeUnit.SECONDS.sleep (1);
                } catch (InterruptedException e) {
                    e.printStackTrace ();
                }
            }
        },"t1").start ();

        new Thread ( () -> {
            while (true){
                if(test01.size () == 5){
                    break;
                }
                System.out.println ("t2 结束");
            }
        },"t2").start ();
    }
}
